﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;

namespace WQDSkin.Core.Converters
{
    /// <summary>
    /// 鼠标移入控件背景色
    /// </summary>
    public class MouseOverBackgroundConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is SolidColorBrush && ((SolidColorBrush)value).Color != Colors.Red)
            {
                SolidColorBrush colorBrush = (SolidColorBrush)value;
                //SolidColorBrush mouseOverBackGround = new SolidColorBrush(ChangeColor(colorBrush.Color, (float)-0.08));
                SolidColorBrush mouseOverBackGround = new SolidColorBrush(Colors.Red);
                
                parameter = mouseOverBackGround;
                return mouseOverBackGround;
            }

            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }

        public Color ChangeColor(Color color, float correctionFactor)
        {
            // 加深：correctionFactor<0 
            // 变亮：correctionFactor > 0 
            // -1.0f <= correctionFactor <= 1.0f

            float red = (float)color.R;
            float green = (float)color.G;
            float blue = (float)color.B;

            if (correctionFactor < 0)
            {
                correctionFactor = 1 + correctionFactor;
                red *= correctionFactor;
                green *= correctionFactor;
                blue *= correctionFactor;
            }
            else
            {
                red = (255 - red) * correctionFactor + red;
                green = (255 - green) * correctionFactor + green;
                blue = (255 - blue) * correctionFactor + blue;
            }

            if (red < 0) red = 0;
            if (red > 255) red = 255;
            if (green < 0) green = 0;
            if (green > 255) green = 255;
            if (blue < 0) blue = 0;
            if (blue > 255) blue = 255;

            return Color.FromArgb(color.A, (byte)red, (byte)green, (byte)blue);
        }

    }
}
